package cn.dw.mm.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.dw.mm.domain.NewEightSelf;
import cn.dw.mm.service.SelfexaminationService;

@Controller
@RequestMapping("/selfexamination")
public class SelfexaminationController {
	@Autowired
	private SelfexaminationService selfexaminationService;
	
	@ResponseBody
	@RequestMapping(value="/userid2")
	public List<NewEightSelf> selectNewEightSelfsByUserId(Integer userId) {
		return selfexaminationService.selectNewEightSelfsByUserId(userId); 
	}
	
	@ResponseBody
	@RequestMapping(value="/selectEf")
	public List<NewEightSelf> selectNewEightSelfsByEightFields(@RequestBody NewEightSelf newEightSelf) {//用@RequestBody意味着前端要传递json数据
		List<NewEightSelf> result = selfexaminationService.selectNewEightSelfsByEightFields(newEightSelf);
		if (result == null) {
			result = new ArrayList<NewEightSelf>();
		}
		return result; 
	}
	
	@ResponseBody
	@RequestMapping(value="/selectUser")
	public List<NewEightSelf> selectNewEightSelfsByUser(@RequestBody NewEightSelf newEightSelf) {
		return selfexaminationService.selectNewEightSelfsByUser(newEightSelf);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public int deleteById(@RequestParam("userId_selfExaminationId") String userId_selfExaminationId) {
		int deleteFlag = selfexaminationService.deleteById(userId_selfExaminationId);
		if (deleteFlag != -1) {
			return 1;
		} else {
			return -1;
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public int insert(HttpServletRequest request) throws IllegalStateException, IOException {
		return selfexaminationService.insert(request);
	}
	
	
	
	@ResponseBody
	@RequestMapping(value="/countByDepartment")
	public List<Map<String, Object> > countByDepartment() {
		return selfexaminationService.countByDepartment(); 
	}
	
	@ResponseBody
	@RequestMapping(value="/countByDomain")
	public List<Map<String, Object> > countByDomain() {
		return selfexaminationService.countByDomain(); 
	}
	
	@ResponseBody
	@RequestMapping(value="/countByEightfield")
	public List<Map<String, Object> > countByEightfield() {
		return selfexaminationService.countByEightfield(); 
	}
	
	@ResponseBody
	@RequestMapping(value="/countByEightfieldsIndex")
	public List<Integer> countByEightfieldIndex(@RequestParam("eightFieldsIndexs") List<Integer> eightFieldsIndexs) {
		List<Integer> resultList = new ArrayList<Integer>();
		Integer[] eightFieldsIndexsArray = eightFieldsIndexs.toArray(new Integer[eightFieldsIndexs.size()]); 
		for (int i = 0; i < eightFieldsIndexsArray.length; i++) {
			resultList.add(selfexaminationService.countByEightfieldIndex(eightFieldsIndexsArray[i]));
		}
		return resultList;
	}
}
